package controller;

import bean.Users;
import service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class LoginController extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username=req.getParameter("username");
        String password=req.getParameter("password");
        System.out.println(username);
        System.out.println(password);

        Users users=new Users();
        users.setName(username);
        users.setPwd(password);

        UserService.login(users);


        resp.setContentType("text/html;charset=utf-8");
        if (users.getId()==null){
            resp.getWriter().write("用户名或密码错误，请重新输入");
            resp.setHeader("refresh","5;login.jsp");
        }else if(users.getId()==-1){
            resp.getWriter().write("系统崩溃了，请稍后再试");
            resp.setHeader("refresh","5;login.jsp");
        }else{
            req.getSession().setAttribute("user",users);
            resp.sendRedirect("HomeController");
        }
    }
}
